package com.example.androidproject.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.androidproject.app.adapter.ClassifyAdapter3;

public class RvBottomManager {
    private final String TAG = "GGSTUDY";
    private final Context mContext;
    private final ImageView back_top_btn;
    private ClassifyAdapter3 classifyAdapter3;
    private RecyclerView rv;
    private Task task;
    private boolean loading;
    private boolean end;

    public RvBottomManager(Context mContext, RecyclerView rv, Task task, ImageView back_top_btn, ClassifyAdapter3 classifyAdapter3) {
        this.mContext = mContext;
        this.rv = rv;
        this.task = task;
        this.loading = false;
        this.end = false;
        this.back_top_btn = back_top_btn;
        this.classifyAdapter3 = classifyAdapter3;

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                // 滚动
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    ResultsConstant.isScrolling = true;
                    Glide.with(mContext).pauseRequests();
                }
                // 静止
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    ResultsConstant.isScrolling = false;
                    Glide.with(mContext).resumeRequests();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (rv.getLayoutManager().findViewByPosition(0) != null) {
                    if (back_top_btn.getVisibility() == View.VISIBLE) {
                        back_top_btn.setVisibility(View.GONE);
                    }
                } else {
                    if (back_top_btn.getVisibility() == View.GONE) {
                        back_top_btn.setVisibility(View.VISIBLE);
                    }
                }
                if (loading || end) {
                    if (end && Tool.isSlideToBottom(rv)) {
                        Toast.makeText(mContext, "数据见底了呦", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    return;
                } else {
                    if (Tool.isSlideToBottom(rv)) {
                        ResultsConstant.isLoading = true;
//                        loading = true;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                task.work();
                            }
                        }).start();
                    }
                }
            }
        });
    }

    public RvBottomManager(Context mContext, RecyclerView rv, Task task, ImageView back_top_btn) {
        this.mContext = mContext;
        this.rv = rv;
        this.task = task;
        this.loading = false;
        this.end = false;
        this.back_top_btn = back_top_btn;
//        this.classifyAdapter3 = classifyAdapter3;

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                // 滚动
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    ResultsConstant.isScrolling = true;
                    Glide.with(mContext).pauseRequests();
                }
                // 静止
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    ResultsConstant.isScrolling = false;
                    Glide.with(mContext).resumeRequests();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (rv.getLayoutManager().findViewByPosition(0) != null) {
                    if (back_top_btn.getVisibility() == View.VISIBLE) {
                        back_top_btn.setVisibility(View.GONE);
                    }
                } else {
                    if (back_top_btn.getVisibility() == View.GONE) {
                        back_top_btn.setVisibility(View.VISIBLE);
                    }
                }
                if (loading || end) {
                    if (end && Tool.isSlideToBottom(rv)) {
                        Toast.makeText(mContext, "数据见底了呦", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    return;
                } else {
                    if (Tool.isSlideToBottom(rv)) {
                        ResultsConstant.isLoading = true;
//                        loading = true;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                task.work();
                            }
                        }).start();
                    }
                }
            }
        });
    }

    public boolean isLoading() {
        return loading;
    }

    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    public boolean isEnd() {
        return end;
    }

    public void setEnd(boolean end) {
        this.end = end;
    }

    public RecyclerView getRv() {
        return rv;
    }

    public void setRv(RecyclerView rv) {
        this.rv = rv;
    }

    public interface Task {
        void work();
    }
}
